#include "stdafx.h"


void PrintErrorListing(CGcontext context)
{
	const char* error_text = cgGetLastListing(context);
	if (error_text)
		printf(error_text);
}

const char* GetFileName(const char* path)
{
	size_t length = strlen(path);
	const char* filename = path;
	for (size_t i = 0; i < length; ++i)
	{
		if (path[i] == '\\' || path[i] == '/')
			filename = path + i + 1;
	}
	return filename;
}

int main(int argc, char** argv)
{
	//	check command line arguments count
	if (argc != 2)
	{
		printf("Error: only one argument allowed: CgFX shader filename\n");
		return EXIT_FAILURE;
	}
	const char* filename = argv[1];

	//	init Cg framework
	CGcontext context =  cgCreateContext();
	cgD3D9RegisterStates(context);

	//	create effect from file
	printf("------ Compiling '%s' effect ------\n", GetFileName(filename));
	CGeffect effect = cgCreateEffectFromFile(context, filename, NULL);
	if (effect)
	{
		printf("Compilation .................... succeed\n");

		//	validate techniques
		CGtechnique tech = cgGetFirstTechnique(effect);
		while (tech)
		{
			const char* name = cgGetTechniqueName(tech);
			if (cgValidateTechnique(tech) == CG_FALSE)
			{
				printf("Validating technique '%s'...... failed\n", name);
				PrintErrorListing(context);
			}
			else
			{
				printf("Validating technique '%s'..... succeed\n", name);
			}
			tech = cgGetNextTechnique(tech);
		}

		//	destroy effect
		cgDestroyEffect(effect);
	}
	else
	{
		PrintErrorListing(context);
	}

	//	destroy context
	cgDestroyContext(context);

#ifdef _DEBUG
	_getch();
#endif

	return 0;
}